package com.winning.ds.role.service.impl;

import com.winning.ds.role.dao.AppDao;
import com.winning.ds.role.entity.AppEntity;
import com.winning.ds.role.entity.MenuEntity;
import com.winning.ds.role.entity.SjqxEntity;
import com.winning.ds.role.service.AppService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.util.List;
import java.util.Map;

/**
 * @program: plat
 * @description:
 * @author: lxd
 * @create: 2018-07-24 09:34
 */
@Service("AppService")
public class AppServiceImpl implements AppService {
  @Autowired private AppDao appDao;

  // 查询应用列表
  @Override
  public List<AppEntity> queryAppList(Map<String, Object> queryParams) {
    return appDao.queryList(queryParams);
  }

  // 查询应用
  @Override
  public AppEntity queryApp(String yydm) {
    return appDao.queryApp(yydm);
  }

  // 保存
  @Override
  @Transactional
  public void save(AppEntity app) {
    MenuEntity mu = new MenuEntity();
    mu.setMcode(app.getYydm());
    mu.setMcaption(app.getYymc());
    mu.setPcode("");
    mu.setActive("Y");
    mu.setCdjb("0");
    mu.setYzjd("0");
    mu.setApplicationid(app.getYydm());
    appDao.save(app);
    appDao.insertGncd(mu);
  }

  // 更新
  @Override
  @Transactional
  public void update(AppEntity app) {
    appDao.update(app);
    // appDao.updateGncdJlztByAppid(app);
  }

  // 删除
  @Override
  @Transactional
  public void delApp(String yydm) {
    appDao.delete(yydm);
  }

  // 查询数据权限
  @Override
  public List<SjqxEntity> querySjqx(Map<String, Object> queryParams) {
    return appDao.querySjqx(queryParams);
  }

  // 保存数据权限
  @Override
  public void saveSjqx(SjqxEntity sjqx) {
    appDao.saveSjqx(sjqx);
  }

  @Override
  public void updateSjqx(SjqxEntity sjqx) {
    appDao.updateSjqx(sjqx);
  }

  @Override
  public void deleteSjqx(String sjdm) {
    appDao.deleteSjqx(sjdm);
  }

  @Override
  public byte[] getFileLogo() {
    List result = appDao.getFileLogo();
    try {
      if (result.size() > 0 && ((Map) result.get(0)).get("FILESTR") != null) {
        return (byte[]) (((Map) result.get(0)).get("FILESTR"));
      }
      return null;
    } catch (ClassCastException e) {
      return blobToBytes((Blob) (((Map) result.get(0)).get("FILESTR")));
    }
  }

  @Override
  public byte[] queryLoginLogo() {
    List result = appDao.queryLoginLogo();
    try {
      if (result.size() > 0 && ((Map) result.get(0)).get("FILESTR") != null) {
        return (byte[]) (((Map) result.get(0)).get("FILESTR"));
      }
      return null;
    } catch (ClassCastException e) {
      return blobToBytes((Blob) (((Map) result.get(0)).get("FILESTR")));
    }
  }

  @Override
  public byte[] queryTitleLogo() {
    List result = appDao.queryTitleLogo();
    try {
      if (result.size() > 0 && ((Map) result.get(0)).get("FILESTR") != null) {
        return (byte[]) (((Map) result.get(0)).get("FILESTR"));
      }
      return null;
    } catch (ClassCastException e) {
      return blobToBytes((Blob) (((Map) result.get(0)).get("FILESTR")));
    }
  }

  private static byte[] blobToBytes(Blob blob) {
    InputStream is = null;
    byte[] b = null;
    try {
      is = blob.getBinaryStream();
      b = new byte[(int) blob.length()];
      is.read(b);
      return b;
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        is.close();
        is = null;
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return b;
  }

  @Override
  public void doEnable(String yydm) {
    appDao.doEnable(yydm);
  }
}
